from src.main.server.mapper.CreateMysqlCon import CreateMysqlCon


class LoginImpl:
    def __init__(self):
        self.con = CreateMysqlCon().get_con()
        if not self.con:
            raise ConnectionError("数据库连接失败，请检查配置或网络")
        self.cursor = self.con.cursor()

    def login(self, vol_object):
        """登录操作"""
        SQL = ("select * from volunteers_info where "
                     "username = %s and "
                     "password = %s")
        self.cursor.execute(SQL, (vol_object.username, vol_object.password))
        return self.cursor.fetchall()

    def register(self, vol_object):
        """注册操作"""
        Register_SQL = ("insert into volunteers_info (username, password, age, gender, pho_num, email) "
                        "values "
                        "(%s, %s, %s, %s, %s, %s)")
        self.cursor.execute(Register_SQL, (vol_object.username,
                                           vol_object.password,
                                           vol_object.age,
                                           vol_object.gender,
                                           vol_object.phone_num,
                                           vol_object.email)
        )